package br.com.sistema.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.sistema.conexao.Entity;
import br.com.sistema.models.Municipio;
import br.com.sistema.models.Pais;

public class MunicipioDao implements IDao<Municipio> {

	@Override
	public Municipio getById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Municipio> getByCriteria(Municipio o) throws Exception {
		
		try {
			
			EntityManager em = Entity.createEm();
			List<Municipio> list;
			
			try {
				
				CriteriaBuilder cb = em.getCriteriaBuilder();
				CriteriaQuery c = cb.createQuery(Municipio.class);
				Root<Municipio> a = c.from(Municipio.class);
				c.select(a);
				
				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if(o.getEstado() != null){
					predicates.add(cb.equal(a.<Pais>get("estado"), o.getEstado()));
				}
				
				if(o.getMuninome() != null){
					predicates.add(cb.like(a.<String>get("muninome"), "%"+o.getMuninome()+"%"));
				}
				
				if(o.getMunicodnum() != null){
					predicates.add(cb.like(a.<String>get("municodnum"), "%"+o.getMunicodnum()+"%"));
				}
				
				c.where(cb.and(predicates.toArray(new Predicate[]{})));
				
				TypedQuery<Municipio> query = em.createQuery(c);
				list = query.getResultList();
				
				return list;

			} catch (Exception e) {
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}

	}

	@Override
	public void remove(List<Municipio> list) throws Exception {
		try {
			EntityManager em = Entity.createEm();
			try {
				em.getTransaction().begin();
				for(Municipio p : list){
					Municipio i = em.find(Municipio.class, p.getMuniid());
					em.remove(i);
				}
				em.getTransaction().commit();
			} catch (Exception e) {
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public List<Municipio> getAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Municipio manter(Municipio o) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Municipio insere(Municipio o) throws Exception {
		try {
			EntityManager em = Entity.createEm();
			try {
				em.getTransaction().begin();
				em.persist(o);
				em.getTransaction().commit();
				return o;
			} catch (Exception e) {
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Municipio altera(Municipio o) throws Exception {
try {
			
			EntityManager em = Entity.createEm();
			Municipio municipio = em.find(Municipio.class, o.getMuniid());
			
			try {
				
				em.getTransaction().begin();
				municipio.setEstado(o.getEstado());
				municipio.setMuninome(o.getMuninome());
				municipio.setMunicodnum(o.getMunicodnum());
				municipio.setXusuario(o.getXusuario());
				em.persist(municipio);
				em.getTransaction().commit();
				
				return municipio;
				
			} catch (Exception e) {
				
				if(em.getTransaction().isActive()){
					em.getTransaction().rollback();
				}
				
				throw e;
				
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	
	
	
}
